// pages/change/change.js
const app = getApp()
const {
  wxPost,
  imgHost,
  wxGet,
  commonShare,
  getJwt
} = require('../../utils/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: null,
    calcPicker: ['最新报价利率（LPR）', '基准利率', '基准利率下限（7折）', '基准利率上限（1.1倍）', '基准利率上限（1.05倍）', '基准利率下限（85折）'],
    pay: null,
    payPicker: ['3成', '4成', '5成', '6成', '7成', '8成', '9成'],
    limits: [],
    calcTab: 0,
    loanYears: 24,
    commercialRate: 4.65,
    commercialAmount: 100,
    accumulationFundAmount: 100,
    accumulationFundRate: 3.25,
    payWay: 0
  },
  moneyRateChange(e) {
    const {
      type
    } = e.currentTarget.dataset
    const commercial = [4.65, 4.9, 3.43, 5.39, 5.15, 4.17]
    const accumulationFund = [3.25, 3.25, 2.28, 3.57, 3.41, 2.76]
    this.setData({
      [`${type}Index`]: e.detail.value,
      [`${type}`]: type === 'commercialRate' ? commercial[e.detail.value] : accumulationFund[e.detail.value]
    })
  },
  PayChange(e) {
    this.setData({
      pay: e.detail.value
    })
  },
  onLoad: function (options) {
    let array = []
    for (let x = 0; x < 30; x++) {
      array.push(`${x+1}年(${(x+1)*12}期)`)
    }
    this.setData({
      limits: array
    })
  },
  setValue: function (e) {
   // console.info(e)
    const {   key } = e.currentTarget.dataset
    const {  value } = e.detail
    if (key) {
      this.setData({
        [`${key}`]: value
      })
    }
    if(key==='payWay'){
        this.setData({
        monthPay: '',
        payAmount: '',
        accrual: '',
        decrease: '',
        month: '',
        loanAmount: ''
      })
    }
  },
  loanCalc() {
    const {
      calcTab,
      loanYears,
      commercialRate,
      commercialAmount,
      accumulationFundAmount,
      accumulationFundRate,
      payWay
    } = this.data

    let month = 0
    let monthPay, payAmount, accrual, decrease
    if (calcTab === 0 || calcTab === 2) {
      if (!commercialAmount || isNaN(commercialAmount) || !commercialRate || isNaN(commercialRate)) {
        wx.showToast({
          title: '请填写商业贷款金额以及利率!',
          icon: 'none'
        })
        return
      }
      month = (parseInt(loanYears) + 1) * 12
    }
    if (calcTab === 1 || calcTab === 2) {
      if (!accumulationFundAmount || isNaN(accumulationFundAmount) || !accumulationFundRate || isNaN(accumulationFundRate)) {
        wx.showToast({
          title: '请填写公积金贷款金额以及利率!',
          icon: 'none'
        })
        return
      }
      month = (parseInt(loanYears) + 1) * 12
    }
    if (calcTab === 0) {

    } else if (calcTab === 1) {

    }
    let loanAmount1 = calcTab === 1 ? 0 : commercialAmount * 10000 //商业贷款
    let loanAmount2 = calcTab === 0 ? 0 : accumulationFundAmount * 10000 //公积金贷款
    let loanRate1 = calcTab === 1 ? 0 : commercialRate //商业贷款利率
    let loanRate2 = calcTab === 0 ? 0 : accumulationFundRate //公积金贷款利率


    if (parseInt(payWay) === 0) {
      let {  monthPay: a, payAmount: b, accrual: c } = this.averageCapitalPlusInterest(loanAmount1, loanRate1, month)
      let {  monthPay: A, payAmount: B, accrual: C } = this.averageCapitalPlusInterest(loanAmount2, loanRate2, month)
      console.info(a, A, b, B, c, C)
      monthPay = a + A
      payAmount = b + B
      accrual = c + C
    } else if (parseInt(payWay) === 1) {
      let {  monthPay: a, payAmount: b,  accrual: c, decrease: d } = this.averageCapital(loanAmount1, loanRate1, month)
      let {  monthPay: A,  payAmount: B, accrual: C,  decrease: D } = this.averageCapital(loanAmount2, loanRate2, month)
      monthPay = a + A
      payAmount = b + B
      accrual = c + C
      decrease = d + D
      console.info(a, A, b, B, c, C,d,D)

    }
    payAmount = payAmount.toFixed(2)
    accrual = accrual.toFixed(2)
    monthPay = monthPay.toFixed(2)
    this.setData({
      monthPay,
      payAmount,
      accrual,
      decrease: decrease || '',
      month,
      loanAmount: loanAmount1+loanAmount2
    })
  },
  averageCapitalPlusInterest(amount, rate, month) {
    let monthPay = amount * (rate / 100 / 12) * Math.pow((1 + rate / 100 / 12), month) / (Math.pow((1 + rate / 100 / 12), month) - 1)
    let payAmount = monthPay * month //总金额
    let accrual = payAmount - amount //利息
    return {
      monthPay: monthPay || 0,
      payAmount: payAmount || 0,
      accrual: accrual || 0
    }
  },
  averageCapital(amount, rate, month) {
    let capital = (amount / month);
    let firstMonth = capital + (amount - 0) * rate / 12 / 100
    let secondMonth = capital + (amount - capital) * rate / 12 / 100
    let accrual = (month + 1) * amount * rate / 100 / 12 / 2
    let payAmount = amount + accrual
    let decrease = (firstMonth - secondMonth).toFixed(2)
    let monthPay = firstMonth
    return {
      monthPay: monthPay || 0,
      payAmount: payAmount || 0,
      accrual: accrual || 0,
      decrease:parseFloat(decrease)
    }
  },
  onShareAppMessage: function () {
    return commonShare()
  },
  switchTab: function (e) {
    const {
      status
    } = e.currentTarget.dataset
    this.setData({
      calcTab: parseInt(status),
      monthPay: '',
      payAmount: '',
      accrual: '',
      decrease: '',
      month: '',
      loanAmount: ''
    })
  },
  subscribeMessage:function(){
    wx.requestSubscribeMessage({
      tmplIds: ['q_-JQ-4y6_UrwCOyMr7jlqWKW-8siziqCrAhDTiRoA0'],
      success (res) {
        console.info(res)
       },
       fail:(error)=>{
        console.info(error)
       }
    })
  }
})